package com.foton.logistic.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.foton.logistic.R;
import com.foton.logistic.listener.IOnItemClickListener;
import com.foton.logistic.model.CarrierResult;
import com.foton.logistic.view.ultimate.UltimateViewAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by wuhaocong on 2017/7/20.
 */

public class SelectAdapter extends UltimateViewAdapter {
    public Context context;
    public List<CarrierResult.DataEntity> list=new ArrayList<>();
    private IOnItemClickListener iOnItemClickListener;
    private CarrierResult.DataEntity dataEntity;

    public SelectAdapter(Context context, List<CarrierResult.DataEntity> list) {
        this.context = context;
        this.list = list;

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent) {
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        View v = null;
        RecyclerView.ViewHolder holer = null;
        v = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.ft_adapter_select, null);
        v.setLayoutParams(layoutParams);
        holer = new MyViewHolder(v);
        return holer;
    }
    public void setIOnItemClickListener(IOnItemClickListener iOnItemClickListener) {
        this.iOnItemClickListener = iOnItemClickListener;
    }
    @Override
    public int getAdapterItemCount() {
        return list.size();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (position < getItemCount() && (customHeaderView != null ? position <= list.size() : position < list.size()) && (customHeaderView != null ? position > 0 : true)) {
            MyViewHolder viewHolder = (MyViewHolder) holder;
            dataEntity =list.get(position);
            viewHolder.name.setText(dataEntity.name);
            viewHolder.num.setText(dataEntity.number);
            viewHolder.item.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    if (iOnItemClickListener !=null){
                        iOnItemClickListener.onItemClick(position);
                    }
                }
            });
        }


    }
    class MyViewHolder extends RecyclerView.ViewHolder {
        @InjectView(R.id.ft_adapter_select_name)
        TextView name;
        @InjectView(R.id.ft_adapter_select_num)
        TextView num;
        @InjectView(R.id.ft_adapter_select)
        LinearLayout item;
        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this, itemView);
        }
    }

}
